package com.gscitysfy.cus.drain.modules.reserveplan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gscitysfy.cus.drain.modules.reserveplan.entity.DrainReservePlanMnCar;
import com.gscitysfy.cus.drain.modules.reserveplan.entity.DrainReservePlanMnMaterial;
import com.gscitysfy.cus.drain.modules.reserveplan.mapper.DrainReservePlanMnCarMapper;
import com.gscitysfy.cus.drain.modules.reserveplan.mapper.DrainReservePlanMnMaterialMapper;
import com.gscitysfy.cus.drain.modules.reserveplan.service.IDrainReservePlanMnMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shenglin
 * @since 2022-11-15 14:28:05
 */
@Service
public class DrainReservePlanMnMaterialServiceImpl extends ServiceImpl<DrainReservePlanMnMaterialMapper, DrainReservePlanMnMaterial> implements IDrainReservePlanMnMaterialService {

    @Autowired
    private DrainReservePlanMnMaterialMapper drainReservePlanMnMaterialMapper;

    @Override
    public void deletByInfoId(List<String> infoIds) {
        LambdaQueryWrapper<DrainReservePlanMnMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DrainReservePlanMnMaterial::getInfoId,infoIds);
        List<DrainReservePlanMnMaterial> drainReservePlanMnMaterials = drainReservePlanMnMaterialMapper.selectList(queryWrapper);
        for (DrainReservePlanMnMaterial drainReservePlanMnMaterial : drainReservePlanMnMaterials) {
            drainReservePlanMnMaterial.setCmUsable(true);
        }
        this.updateBatchById(drainReservePlanMnMaterials);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int saveBathReservePlanMnMaterialInfo(List<DrainReservePlanMnMaterial> drainReservePlanMnMaterials) {
        if(!drainReservePlanMnMaterials.isEmpty()){
            //删除原有的应急材料信息
            LambdaQueryWrapper<DrainReservePlanMnMaterial> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DrainReservePlanMnMaterial::getInfoId,drainReservePlanMnMaterials.get(0).getInfoId());
            List<DrainReservePlanMnMaterial> old = drainReservePlanMnMaterialMapper.selectList(queryWrapper);
            for (DrainReservePlanMnMaterial drainReservePlanMnMaterial : old) {
                drainReservePlanMnMaterial.setCmUsable(true);
            }
            this.updateBatchById(old);
            int i=0;
            for (DrainReservePlanMnMaterial reservePlanMnMaterial : drainReservePlanMnMaterials) {
                //预案应急材料的预案id不能为空
                if(reservePlanMnMaterial!=null&& reservePlanMnMaterial.getInfoId()!=null){
                    i+=drainReservePlanMnMaterialMapper.insert(reservePlanMnMaterial);
                }
            }
            return i;
        }
        return 0;
    }

    @Override
    public List<DrainReservePlanMnMaterial> getReservePlanMnMaterialInfoList(String infoId,String level) {
        LambdaQueryWrapper<DrainReservePlanMnMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DrainReservePlanMnMaterial::getInfoId,infoId);
        queryWrapper.eq(DrainReservePlanMnMaterial::getCmUsable,false);
        if(StringUtils.isNotBlank(level)){
            queryWrapper.eq(DrainReservePlanMnMaterial::getLevel,level);
        }
        return drainReservePlanMnMaterialMapper.selectList(queryWrapper);
    }
}
